import { getStore } from './storage';
// 统一请求路径前缀
let baseApi = '';
export const getRequest = (axios,url, params) => {
    let accessToken = getStore('ACCESS_TOKEN')||'';
    return axios({
        method: 'get',
        url: `${baseApi}${url}`,
        params: params,
        headers: {
            'accessToken': accessToken
        }
    });
};

export const postRequest = (axios,url, params) => {
    let accessToken = getStore('ACCESS_TOKEN');
    return axios({
        method: 'post',
        url: `${baseApi}${url}`,
        data: params,
        transformRequest: [function (data) {
            let ret = '';
            for (let it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
            }
            ret = ret.substring(0, ret.length - 1);
            return ret;
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'accessToken': accessToken
        }
    });
};
export const deleteRequest = (axios,url, params) => {
    let accessToken = getStore('ACCESS_TOKEN');
    return axios({
        method: 'delete',
        url: `${baseApi}${url}`,
        data: params,
        transformRequest: [function (data) {
            let ret = '';
            for (let it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
            }
            ret = ret.substring(0, ret.length - 1);
            return ret;
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'accessToken': accessToken
        }
    });
};
export const postJsonRequest = (axios,url, params) => {
	let accessToken = getStore('ACCESS_TOKEN');
	return axios({
	    method: 'post',
	    url: `${baseApi}${url}`,
	    data: params,
	    headers: {
	        'Content-Type': 'application/json',
	        'accessToken': accessToken
	    }
	});
};
export const putRequest = (axios,url, params) => {
    let accessToken = getStore('ACCESS_TOKEN');
    return axios({
        method: 'put',
        url: `${baseApi}${url}`,
        data: params,
        transformRequest: [function (data) {
            let ret = '';
            for (let it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
            }
            ret = ret.substring(0, ret.length - 1);
            return ret;
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'accessToken': accessToken
        }
    });
};

export const postBodyRequest = (axios,url, params) => {
    let accessToken = getStore('ACCESS_TOKEN');
    return axios({
        method: 'post',
        url: `${baseApi}${url}`,
        data: params,
        headers: {
            'accessToken': accessToken
        }
    });
};

/**
 * 无需token验证的GET请求 避免旧token过期导致请求失败
 * @param {*} url
 * @param {*} params
 */
export const getRequestWithNoToken = (axios,url, params) => {
    return axios({
        method: 'get',
        url: `${baseApi}${url}`,
        params: params
    });
};

export const postRequestWithNoToken = (axios,url, params) => {
    return axios({
        method: 'post',
        url: `${baseApi}${url}`,
        data: params,
        transformRequest: [function (data) {
            let ret = '';
            for (let it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
            }
            ret = ret.substring(0, ret.length - 1);
            return ret;
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
};
